package com.analytical.www.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.analytical.www.R
import com.analytical.www.bean.BeanSample
import kotlin.properties.Delegates


/**
 *@Auther Shawn
 *创建时间：2023/12/24 22:45
 */
class AdapterSampleInput(private val mContext:Context,clickListener: ItemClickListener) : RecyclerView.Adapter<AdapterSampleInput.SampleHolder>() {
    lateinit var mData: MutableList<BeanSample>
    private val itemClickListener:AdapterSampleInput.ItemClickListener = clickListener
    var selPos by Delegates.observable(0) { _, old, new ->
        notifyItemChanged(old)
        notifyItemChanged(new)
    }

    interface ItemClickListener{
        fun click(position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleHolder {
        val view =
            LayoutInflater.from(mContext).inflate(R.layout.item_sample, parent, false)
        return SampleHolder(view)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: SampleHolder, position: Int) {
        holder.mName.text = mData[position].name
        if (selPos == position) {
            holder.mName.setBackgroundColor(Color.parseColor("#FFFFFF"))
        } else {
            holder.mName.setBackgroundColor(0)
        }
        holder.mName.setOnClickListener {
            itemClickListener.click(position)
        }
    }

    inner class SampleHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mName:TextView = itemView.findViewById(R.id.tv_name)
    }
}